package com.sdry.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.Loan;
import com.sdry.entity.LoanExample;
import com.sdry.entity.LoanExample.Criteria;
import com.sdry.service.LoanServise;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: LoanController
 * @Description: 借调管理
 * @author jyy
 * @date 2018年7月26日
 * @version V1.0
 */
@Controller
@RequestMapping("/loan")
public class LoanController extends BaseController{

    @Resource
    private LoanServise loanServise;


    /**
     * 去借调单列表页面
     * @return
     */
        @RequestMapping("/pageToLoan")
        public String pageToLoan() {

            return "/loan/loan";
    }

    /**
     * 添加借调管理
     * @param response 响应
     * @param loan 借调实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertLoan")
    public void insertLoan( HttpServletResponse response, Loan loan) throws Exception {

        //添加
        int affact = loanServise.insert(loan);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询借调信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param Loan 借调信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectLoanList")
    public void selectLoanList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "limit",required = false)int limit,
                                   @RequestParam(value = "offset",required = false)int offset,
                                   Loan loan) throws Exception {
       
    	PageHelper.startPage(offset, limit);
    	
    	LoanExample example = new LoanExample();
    	Criteria createCriteria = example.createCriteria();
    	if(loan.getCremark3() != null && loan.getCremark3() != "") {
    		createCriteria.andCremark3EqualTo(loan.getCremark3());
    	}
    	
    	if(loan.getCremark9() != null && loan.getCremark9() != "") {
    		createCriteria.andCremark9Like("%"+loan.getCremark9()+"%");
    	}
    	
		List<Loan> list = loanServise.selectByExample(example);
		
		PageInfo<Loan> info = new PageInfo<Loan>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据id查询借调信息
     * @param response 响应
     * @param id 借调id
     * @throws Exception 异常
     */
    @RequestMapping("/selectLoanById")
    public void selectLoanById(HttpServletResponse response, Long id) throws Exception {

        Loan loan = loanServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(loan);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改借调信息
     * @param response 响应
     * @param Loan 借调信息
     * @throws Exception 异常
     */
    @RequestMapping("/editLoan")
    public void editLoan( HttpServletResponse response,
                             Loan Loan) throws Exception {

        //修改
        int affct = loanServise.updateByPrimaryKey(Loan);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除
     * @param response 响应
     * @param ids 借调id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteLoan")
    public void deleteLoan( HttpServletResponse response, String ids) throws Exception {

        int affact = loanServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }


}
